﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace Webserver
{
    class Server
    {
        public Server()
        {

        }

        public void Run()
        {
            // Create a socket to receive incomming requests
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(new IPEndPoint(IPAddress.Loopback, 8080));
            socket.Listen(10);


            //while (true) { 
                // Wait for a client to connect and receive the request
                Socket client = socket.Accept();

                // Use Send and Receive methods on client to send and receive data to the browser
                //Date: Tue, 15 Nov 1994 08:12:31 GMT
                string responseHeader = "HTTP/1.1 200 OK\n\rCache-Control: no-cache\n\rDate: Tue, 15 Nov 1994 08:12:31 GMT\n\rContent-Type: text/html\n\r\n\r";
                string responseBody = "<html><head><title>Jaspers webserver</title></head><body><h1>Hep hey!</h1></body></html>";

                byte[] reply = Encoding.ASCII.GetBytes(responseHeader + responseBody);
                client.Send(reply);

                // Close and dispose the client socket to terminate the connection to the browser
                client.Close();
                client.Dispose();
            //}

            // Close and dispose the socket socket to stop listening for connections
            socket.Close();
            socket.Dispose();
        }
    }
}
